﻿using System;
using System.Collections.Generic;

namespace FlashPay.Service.Impl
{
    using FlashPay.DAO.Interface;
    using FlashPay.EF.Models;
    using FlashPay.Entity;
    using FlashPay.Entity.Parameter;
    using FlashPay.Entity.Request.Log;
    using FlashPay.Service.Interface;
    using FlashPay.Entity.Response.Logs;

    /// <summary>
    /// 日志业务接口实现
    /// </summary>
    public class LogServiceImpl:LogService
    {
        #region 注入
        /// <summary>
        /// 用户数据接口
        /// </summary>
        private readonly LogDao _logDao;

        /// <summary>
        /// 注入
        /// </summary>
        /// <param name="menu">菜单数据接口</param>
        public LogServiceImpl(LogDao logDao)
        {
            _logDao = logDao;
        }
        #endregion

        /// <summary>
        /// 新增日志
        /// </summary>
        /// <param name="userInfo">新增对象</param>
        /// <returns></returns>
        public void AddLog(LogRecordRequest request)
        {
            var log = new LogRecord()
            {
                Ip = request.Ip,
                LogType = request.LogType.GetHashCode(),
                CreateName = request.CreateName,
                CreateDate = DateTime.Now,
                CreateUid = request.CreateUid,
                LogRemark = request.LogRemark,
                CompanyId = request.CompanyId,
                RequestUrl = request.RequestUrl,
                RequestData = request.RequestData
            };
            _logDao.Insert(log);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<LogRecordResponse></returns>
        public PagedList<LogRecordResponse> GetPager(LogQuery query)
        {
            return _logDao.GetPager(query);
        }

        //删除
        public JResult Delete(int LogId) {
            JResult returnInfo = new JResult();
            if (LogId <= 0)
            {
                returnInfo.ErrorMessage = "参数输入错误!";
                returnInfo.Success = false;
            }
            var rows= _logDao.Delete(LogId);
            if (rows <= 0)
            {
                returnInfo.ErrorMessage = "删除失败!";
                returnInfo.Success = false;
            }
            else
            {
                returnInfo.Success = true;
                returnInfo.SuccessMessage = "删除成功";
            }
            return returnInfo;
        }

        /// <summary>
        /// 获取日志类型列表
        /// </summary>
        /// <returns>List<string></returns>
        public List<string> GetLogTypes()
        {
            var resultDAO = _logDao.GetLogTypes();
            return resultDAO;
        }

        /// <summary>
        /// 获取IP列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>List<string></returns>
        public JResult<List<string>> GetIPs(LogQuery query)
        {
            JResult<List<string>> result = new JResult<List<string>>();
            var resultDAO = _logDao.GetList(query, 1, 10);
            if (resultDAO != null)
            {
                result.Success = true;
                result.Data = resultDAO;
            }
            return result;
        }

        /// <summary>
        /// 根据订单号获取操作日志
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<LogRecordResponse></returns>
        public DataGrid<LogRecordResponse> GetListByOrderNo(GetListByOrderNoQuery query)
        {
            return _logDao.GetListByOrderNo(query);
        }
    }
}
